home *** CD-ROM | disk | FTP | other *** search
- /* xlstr - xlisp string builtin functions */
-
- #ifdef CI_86
- #include "a:stdio.h"
- #include "xlisp.h"
- #endif
-
- #ifdef AZTEC
- #include "a:stdio.h"
- #include "xlisp.h"
- #endif
-
- #ifdef unix
- #include <stdio.h>
- #include <xlisp.h>
- #endif
-
-
- /* external variables */
-
- extern struct node *xlstack;
-
-
- /* external procedures */
-
- extern char *strcat();
-
-
- /*********************************
- * xstrlen - length of a string *
- *********************************/
-
- static struct node *xstrlen(args)
- struct node *args;
- {
- struct node *oldstk,arg,*val;
- int total;
-
- oldstk = xlsave(&arg,NULL);
- arg.n_ptr = args;
- total = 0;
-
- while (arg.n_ptr != NULL)
- total += strlen(xlevmatch(STR,&arg.n_ptr)->n_str);
-
- xlstack = oldstk;
-
- val = newnode(INT);
- val->n_int = total;
-
- return (val);
- }
-
-
- /*********************************************
- * xstrcat - concatenate a bunch of strings *
- *********************************************/
-
-
- static struct node *xstrcat(args)
- struct node *args;
- {
- /* this routine does it the dumb way -- one at a time */
- struct node *oldstk,arg,val,rval;
- int newlen;
- char *result,*argstr,*newstr;
-
- oldstk = xlsave(&arg,&val,&rval,NULL);
- arg.n_ptr = args;
- rval.n_ptr = newnode(STR);
- rval.n_ptr->n_str = result = stralloc(0);
- *result = 0;
-
- while (arg.n_ptr != NULL) {
- val.n_ptr = xlevmatch(STR,&arg.n_ptr);
- argstr = val.n_ptr->n_str;
- newlen = strlen(result) + strlen(argstr);
- newstr = stralloc(newlen);
- strcpy(newstr,result);
- strfree(result);
- rval.n_ptr->n_str = result = strcat(newstr,argstr);
- }
-
- xlstack = oldstk;
- return (rval.n_ptr);
- }
-
-
- /********************************
- * substr - return a substring *
- ********************************/
-
- static struct node *substr(args)
- struct node *args;
- {
- struct node *oldstk,arg,src,val;
- int start,forlen,srclen;
- char *srcptr,*dstptr;
-
- oldstk = xlsave(&arg,&src,&val,NULL);
- arg.n_ptr = args;
-
- src.n_ptr = xlevmatch(STR,&arg.n_ptr);
- srcptr = src.n_ptr->n_str;
- srclen = strlen(srcptr);
-
- start = xlevmatch(INT,&arg.n_ptr)->n_int;
-
- if (arg.n_ptr != NULL)
- forlen = xlevmatch(INT,&arg.n_ptr)->n_int;
- else
- forlen = srclen; /* use len and fix below */
-
- xllastarg(arg.n_ptr);
-
- if (start + forlen > srclen)
- forlen = srclen - start + 1;
-
- if (start > srclen)
- {
- start = 1;
- forlen = 0;
- }
-
- val.n_ptr = newnode(STR);
- val.n_ptr->n_str = dstptr = stralloc(forlen);
-
- for (srcptr += start-1; forlen--; *dstptr++ = *srcptr++)
- ;
-
- *dstptr = 0;
-
- xlstack = oldstk;
- return (val.n_ptr);
- }
-
-
- /*******************************
- * ascii - return ascii value *
- *******************************/
-
- static struct node *ascii(args)
- struct node *args;
- {
- struct node *oldstk,val;
-
- oldstk = xlsave(&val,NULL);
-
- val.n_ptr = newnode(INT);
- val.n_ptr->n_int = *(xlevmatch(STR,&args)->n_str);
-
- xllastarg(args);
-
- xlstack = oldstk;
- return (val.n_ptr);
- }
-
-
- /***********************************************************
- * chr - convert an INT into a one character ascii string *
- ***********************************************************/
-
- static struct node *chr(args)
- struct node *args;
- {
- struct node *oldstk,val;
- char *sptr;
-
- oldstk = xlsave(&val,NULL);
-
- val.n_ptr = newnode(STR);
- val.n_ptr->n_str = sptr = stralloc(1);
- *sptr++ = xlevmatch(INT,&args)->n_int;
- *sptr = 0;
-
- xllastarg(args);
-
- xlstack = oldstk;
- return (val.n_ptr);
- }
-
-
- /**************************************************
- * xatoi - convert an ascii string to an integer *
- **************************************************/
-
- static struct node *xatoi(args)
- struct node *args;
- {
- struct node *val;
- int n;
-
- n = atoi(xlevmatch(STR,&args)->n_str);
-
- xllastarg(args);
-
- val = newnode(INT);
- val->n_int = n;
- return (val);
- }
-
-
- /**************************************************
- * xitoa - convert an integer to an ascii string *
- **************************************************/
-
- static struct node *xitoa(args)
- struct node *args;
- {
- struct node *val;
- char buf[20];
-
- sprintf(buf,"%d",xlevmatch(INT,&args)->n_int);
-
- xllastarg(args);
-
- val = newnode(STR);
- val->n_str = strsave(buf);
- return (val);
- }
-
-
- /**************************************************
- * xlsinit - xlisp string initialization routine *
- **************************************************/
-
- xlsinit()
- {
- xlsubr("strlen",xstrlen);
- xlsubr("strcat",xstrcat);
- xlsubr("substr",substr);
- xlsubr("ascii",ascii);
- xlsubr("chr", chr);
- xlsubr("atoi",xatoi);
- xlsubr("itoa",xitoa);
- }